package com.school.manager.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.school.manager.common.Constants;
import com.school.manager.domaim.SysRole;
import com.school.manager.domaim.SysRolePerm;
import com.school.manager.entity.ResponseEntity;
import com.school.manager.entity.po.SysRolePermissesPo;
import com.school.manager.entity.po.SysRolePo;
import com.school.manager.entity.vo.SysRoleVo;
import com.school.manager.enums.ResponseEnum;
import com.school.manager.enums.StatusEnum;
import com.school.manager.service.ISysRolePermService;
import com.school.manager.service.ISysRoleService;
import com.school.manager.utils.ConvertUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * description：系统角色控制器
 * date：2021/10/133:18 下午
 */

@RestController
@Api(tags = "系统角色")
@RequestMapping(Constants.PROGRAM_PATH + "role")
public class SysRoleController {
	
	@Autowired
    private ISysRoleService sysRoleService;
	
	@Autowired
	private ISysRolePermService rolePermService;

	@PostMapping("add")
    @ApiOperation("新增系统角色")
    @ApiImplicitParam(name = "系统角色", value = "sysRolePo", dataTypeClass = SysRolePo.class)
    public ResponseEntity add(@RequestBody SysRolePo sysRolePo) {
        return ResponseEnum.SUCCESS.success(sysRoleService.save(ConvertUtil.convertObj(sysRolePo, SysRole.class)));
    }


    @PostMapping("edit")
    @ApiOperation("修改系统角色")
    @ApiImplicitParam(name = "系统角色", value = "sysRolePo", dataTypeClass = SysRolePo.class)
    public ResponseEntity edit(@RequestBody SysRolePo sysRolePo) {
        return ResponseEnum.SUCCESS.success(sysRoleService.updateById(ConvertUtil.convertObj(sysRolePo, SysRole.class)));
    }


    @GetMapping("get/{id}")
    @ApiOperation("系统角色详情")
    @ApiImplicitParam(name = "系统角色主键", value = "id", dataTypeClass = String.class)
    public ResponseEntity get(@PathVariable("id") String id) {
        SysRole sysRole = sysRoleService.getById(id);
        return ResponseEnum.SUCCESS.success(ConvertUtil.convertObj(sysRole, SysRoleVo.class));
    }


    @PostMapping("page")
    @ApiOperation("系统角色分页列表")
    @ApiImplicitParam(name = "系统角色Po", value = "sysRolePo", dataTypeClass = SysRolePo.class)
    public ResponseEntity page(@RequestBody SysRolePo sysRolePo) {
        return ResponseEnum.SUCCESS.success(sysRoleService.page(sysRolePo));
    }
    
    @GetMapping("list")
    @ApiOperation("系统角色分页列表")
    public ResponseEntity list() {
    	List<SysRole> list = sysRoleService.list(new LambdaQueryWrapper<SysRole>()
    			.eq(SysRole::getStatus, StatusEnum.ENABLE.getCode()));
        return ResponseEnum.SUCCESS.success(ConvertUtil.convertList(list, SysRoleVo.class));
    }
    
    @PostMapping("permis")
    @ApiOperation("修改系统角色权限")
    @ApiImplicitParam(name = "系统角色权限Po", value = "rolePermissesPo", dataTypeClass = SysRolePermissesPo.class)
    public ResponseEntity permis(@RequestBody SysRolePermissesPo rolePermissesPo) {
    	String roleId = rolePermissesPo.getRoleId();
    	// 删除原有权限
    	rolePermService.remove(new LambdaQueryWrapper<SysRolePerm>()
    			.eq(SysRolePerm::getRoleId, roleId));
    	// 重新设置权限
    	List<SysRolePerm> sysRolePerms = rolePermissesPo.getPermisses().stream().map(permiss -> {
    		SysRolePerm sysRolePerm = new SysRolePerm();
    		sysRolePerm.setRoleId(roleId);
    		sysRolePerm.setPermId(permiss.getId());
    		return sysRolePerm;
    	}).collect(Collectors.toList());
    	rolePermService.saveBatch(sysRolePerms);
        return ResponseEnum.SUCCESS.success();
    }

}
